package com.oscar.ehome.view.setview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.DownBean;
import com.oscar.ehome.presenter.SetPresenter;
import com.oscar.ehome.utils.database.DataBaseDAO;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.messageview.MessageDetilActivity;
import com.oscar.ehome.view.pageview.FrequencyActivity;
import com.oscar.ehome.view.pageview.PdfDetialActivity;
import com.oscar.ehome.view.pageview.StudyActivity;
import com.oscar.ehome.view.pageview.VideoActivity;
import com.oscar.ehome.view.setview.adapter.MyDownLoadAdapter;
import com.oscar.ehome.view.setview.view.DownView;

import org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/7/21
 * 我的下载页
 */

public class MyDownloadActivity extends BasesActivity<DownView, SetPresenter> implements DownView, PullToRefreshBase.OnRefreshListener, AdapterView.OnItemClickListener {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 列表
     */
    @BindView(R.id.down_listview)
    PullToRefreshListView downListview;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;

    SetPresenter setPresenter;
    MyDownLoadAdapter adapter;

    String token = "";
    DataBaseDAO db;

    List<Map<String, Object>> tempData = new ArrayList<Map<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mydownload);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(MyDownloadActivity.this).getSystemConfig().getColor()));
        initView();
        initData();
    }

    @Override
    protected SetPresenter createPresenter() {
        setPresenter = new SetPresenter(this);
        return setPresenter;
    }

    /**
     * 初始化数据
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("下载列表");
        token = SystemConfigFactory.getInstance(MyDownloadActivity.this).getSystemConfig().getToken();

        db = new DataBaseDAO(MyDownloadActivity.this);

        adapter = new MyDownLoadAdapter(this, tempData);
        downListview.setAdapter(adapter);

        onClick();

    }

    /**
     * 请求数据
     */
    public void initData() {
        tempData = db.selectAll("fileDownload");
        if (tempData.size() > 0) {
            Log.e("mwh", tempData + "tempData");
            adapter.setList(tempData);
        }
    }

    public void onClick() {
        downListview.setPullLoadEnabled(true);
        downListview.setOnRefreshListener(this);
        downListview.setOnItemClickListener(this);
    }

    @OnClick(R.id.title_layout_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void DownLoad(DownBean downBean) {
        dismissLoading();
        downListview.onPullDownRefreshComplete();
        downListview.onPullUpRefreshComplete();
    }

    @Override
    public void erroy() {
        dismissLoading();
        downListview.onPullDownRefreshComplete();
        downListview.onPullUpRefreshComplete();
        ToastUtil.show(MyDownloadActivity.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        downListview.onPullDownRefreshComplete();
        downListview.onPullUpRefreshComplete();
        ToastUtil.show(MyDownloadActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(MyDownloadActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(MyDownloadActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        downListview.onPullDownRefreshComplete();
        downListview.onPullUpRefreshComplete();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        downListview.onPullDownRefreshComplete();
        downListview.onPullUpRefreshComplete();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String learnType = MapUtils.getString(tempData.get(position), "learnType");
        if ("video/mp4".equals(learnType)) {//视频
            Intent intent = new Intent(MyDownloadActivity.this, VideoActivity.class);
            intent.putExtra("id", MapUtils.getString(tempData.get(position), "ids"));
            startActivity(intent);
        } else if ("application/pdf".equals(learnType)) {//PDF
            Intent intent = new Intent(MyDownloadActivity.this, PdfDetialActivity.class);
            intent.putExtra("id", MapUtils.getString(tempData.get(position), "ids"));
            startActivity(intent);
        } else if ("audio/mp3".equals(learnType)) {//音频
            Intent intent = new Intent(MyDownloadActivity.this, FrequencyActivity.class);
            intent.putExtra("id", MapUtils.getString(tempData.get(position), "ids"));
            startActivity(intent);
        }
    }
}
